package com.flowCloud.config.exception;

import com.flowCloud.result.ResultModel;
import com.flowCloud.result.ResultTools;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.clazz.kit.BeetlSQLException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Pompey_hpy
 * @Date: 2020/4/1
 * @Description: 全局异常处理类
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {



    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BadRequestParameterException.class)
    @ResponseBody
    public ResultModel bizExceptionHandler(HttpServletRequest req, BadRequestParameterException e){
        log.error("【异常错误】发生业务异常！原因是："+e.getErrorMsg());
        log.error(e.getMessage(), e);
        return ResultTools.fail(5000,e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, NullPointerException e){
        log.error("【异常错误】发生空指针异常！原因是:");
        log.error(e.getMessage(), e);
        return ResultTools.fail(4000,"请求的数据格式不符!");
    }

    /**
     * 处理BeetlSQL异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BeetlSQLException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, BeetlSQLException e){
        log.error("【异常错误】sql参数异常！原因是:");
        log.error(e.getMessage(), e);
        return  ResultTools.fail(4001,"sql参数异常了！");
    }

    /**
     * 处理HttpMessageNotReadableException异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, HttpMessageNotReadableException e){
        log.error("【异常错误】传参异常！原因是:");
        log.error(e.getMessage(), e);
        return  ResultTools.fail(4002,"传参格式错误！");
    }

    /**
     * 处理HttpRequestMethodNotSupportedException异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, HttpRequestMethodNotSupportedException e){
        log.error("【异常错误】请求异常！原因是:");
        log.error(e.getMessage(), e);
        return  ResultTools.fail(4003,"post/get等请求错误！");
    }

    /**
     * 处理MaxUploadSizeExceededException异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, MaxUploadSizeExceededException e){
        log.error("【异常错误】上传异常！原因是:");
        log.error(e.getMessage(), e);
        return  ResultTools.fail(4004,"上传文件超限！");
    }


    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, MethodArgumentNotValidException e){
        log.error("【异常错误】验证异常！原因是:");
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        log.error(e.getMessage(), objectError);
        // 注意哦，这里返回类型是自定义响应体
        return  ResultTools.fail(1001,"参数校验失败"+objectError.getDefaultMessage());
    }

    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResultModel exceptionHandler(HttpServletRequest req, Exception e){
        log.error("【异常错误】未知异常！原因是:");
        log.error(e.getMessage(), e);
        return  ResultTools.fail(5001,"出现未知异常！");
    }



}
